package com.nf.health.app.bodyfat.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nf.health.app.R;
import com.nf.health.app.bodyfat.bean.BodyFatHistoryBean;
import com.nf.health.app.utils.PreferenceHelper;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by hepan on 2016/3/21.
 * The only genius that is worth anything is the genius for hard work
 */
public class BodyFatHistoryDataAdapter extends BaseAdapter {
    private Activity context;
    private BodyFatHistoryBean bean;
    //设备数据集合
    private List<BodyFatHistoryBean.DataBean.DeviceDataBean> deviceDatas;

    public BodyFatHistoryDataAdapter(Activity context, BodyFatHistoryBean bean) {
        this.context = context;
        this.bean = bean;
        deviceDatas = bean.data.deviceData;
    }

    /**
     * 设置适配器数据
     *
     * @param bean
     */
    public void setAdapterData(BodyFatHistoryBean bean) {
        this.bean = bean;
        deviceDatas = bean.data.deviceData;
    }

    @Override
    public int getCount() {
        return deviceDatas.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //取得
        BodyFatHistoryBean.DataBean.DeviceDataBean deviceData = deviceDatas.get(position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.activity_body_fat_history_data_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //设置选中item背景色
        if (position == PreferenceHelper.getInt(PreferenceHelper.BODY_FAT_HISTTORY_ITEM, 1) - 1) {
            holder.llDate.setBackgroundColor(0xffffffff);
            holder.tvDate.setTextColor(context.getResources().getColor(R.color.theme_color));
        } else {
            holder.llDate.setBackgroundColor(0xffb3b3b3);
            holder.tvDate.setTextColor(context.getResources().getColor(R.color.black9));
        }
        if (deviceData.posttime != "") {
            holder.tvDate.setText(deviceData.posttime.substring(5, 16));
        }
        return convertView;
    }


    static class ViewHolder {
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.ll_date)
        LinearLayout llDate;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
